package com.saas.servlet;

import com.saas.service.IAccountService;
import com.saas.service.impl.AccountServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/mylogin")
public class MyLoginServlet extends HttpServlet {

    private IAccountService ias = new AccountServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        System.out.println("this is my login servlet .");

        String username = req.getParameter("username");
        String password = req.getParameter("password");


        boolean flag = ias.login(username, password);

        if (flag) {
            System.out.println("login success");
            req.getRequestDispatcher("main.html").forward(req, resp);
        } else {
            System.out.println("login fail");
            req.getRequestDispatcher("login.html").forward(req, resp);
        }
    }
}
